<?php

namespace App\Models\Flaw;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use App\Models\Flaw\Vuls;

class VulsRecord extends Model
{
    //关联表
    protected  $table = 'vuls_record';
    //可以批量赋值的值
    protected $fillable = ['app_id','vuls_id','vuls_status','content','created_by','updated_by','update_by','delayed_at'];
    //设置主键
    public $primaryKey = 'id';
    //关闭创建日期与更新日期的自动维护
    public $timestamps= true;
    /**
     * validate 表单数据验证
     *
     * @param $data
     * @return bool
     */
    public  static function validate($data){
        return  Validator::make($data, [
            'content'=>'required',
        ],[
                'content.required' => '请填写漏洞相关描述信息！',
            ]
        );
    }

    /**
     * getVulsRecords 获取漏洞记录数
     *
     * @param $vulsId integer 漏洞id
     * @return mixed
     */
    public static function  getVulsRecords($vulsId){
        return DB::table('vuls_record')
            ->leftjoin('users','users.id','=','vuls_record.created_by')
            ->select('vuls_record.*','users.name as account', 'users.real_name as uname')
            ->where('vuls_record.vuls_id','=',$vulsId)
            ->orderby('created_at','asc')
            ->get();
    }

    /**
     * getCreateTime 根据漏洞不同的状态获取所对应的时间，审核时间、确认时间
     * 修复时间、解决时间
     *
     * @param int $vulsId 漏洞id
     * @param int $status 漏洞状态
     * @return mixed
     */
    public static function getCreateTime($vulsId = 0, $status = -1){
        return DB::table('vuls_record')
            ->where([['vuls_id','=',$vulsId],['vuls_status','=',$status]])
            ->value('created_at');
    }

    /**
     * getRepairUseTime  根据漏洞id获取漏洞修复用时
     * 
     * @param int $vulsId 漏洞id
     * @param string $auditTime  审核员审核漏洞发邮件后的时间
     * @return int|string
     */
    public static function getRepairUseTime($vulsId = 0, $auditTime = "0"){
        $repairUseTime = '';
        $isEmpty = DB::table('vuls_record')
            ->where([['vuls_id','=',$vulsId],['vuls_status','=',5]])
            ->get()
            ->isEmpty();
        if(!$isEmpty){
            $repairDate = DB::table('vuls_record')
                ->where([['vuls_id','=',$vulsId],['vuls_status','=',4]])
                ->value('created_at');
            $auditTime = $auditTime ? $auditTime : self::getCreateTime($vulsId, 2);
            $repairUseTime = getDiffDay($repairDate, $auditTime);
        }
        return $repairUseTime;
    }

    /**
     * getOgzRelateInfo 获取机构的相关信息
     * 
     * @param integer $pid 组织机构id
     * @param bool $withTrash  是否选用回收站的值
     * @param bool $isGetId  是获取id集合还是name集合 默认false 返回id集合
     * @param bool $isGetChild  是获取向下获取子元素集合还是向上获取父元素集合 默认：false向上
     * @return array
     */
    public static function getOgzRelateInfo ($pid, $withTrash = false, $isGetId = false, $isGetChild = false, $selFlag = true) {
        $ogzNames = [];
        $selEle = $isGetChild ? 'pid' : 'id'; 
        $OgzInfo = DB::table('ogz')->select(['id','pid','name'])->where($selEle,'=',$pid);
        if($isGetChild && $selFlag){
            $ogzNames[] = DB::table('ogz')->select(['id','pid','name'])->where('id','=',$pid)->value('id');
        }
        if (!$withTrash){
            $OgzInfo = $OgzInfo->whereNull('deleted_at');
        }
        $data = !$isGetChild ? $OgzInfo->first() : $OgzInfo->get();
        if (count($data)>0){
            if(!$isGetChild){
                $ogzNames[] = !$isGetId ? $data->name : $data->id;
                if ($data->pid != 0 && $data->pid != $data->id){
                    $ogzNames = array_merge($ogzNames, self::getOgzRelateInfo($data->pid, $withTrash, $isGetId, $isGetChild));
                }
            }else{
                foreach ($data as $d){
                    $ogzNames[] =$d->id;
                    $ogzNames = array_merge($ogzNames, self::getOgzRelateInfo($d->id, $withTrash, $isGetId, $isGetChild, false));
                }
            }
        }
        return $ogzNames;
    }

    /**
     * 记录
     * @param int $vul_id
     * @param array $attr
     * @return bool
     */
    public static function setRecord ($vul_id,$attr){
        if (intval($vul_id) < 0){
            return false;
        }
        $vul = Vuls::find($vul_id);
        $data = [
            'vuls_id' => $vul->id,
            'app_id'  => $vul->app_id,
            'reward'  => $vul->reward,
            'credit'  => $vul->credit,
            'created_by'=>\Auth::id()
        ];
        if(self::create(array_merge($data,$attr))->id >0){
            return true;
        }
        return false;
    }

}
